package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogComment;


public class GenericBlogCommentDAO extends AutoKeys implements org.jetnuke.dao.BlogCommentDAO {
	public BlogComment retrieveByPK(int id)throws Exception{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogCommentModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		BlogComment s=new BlogComment();
		if (rs.next()) {
			fillBlogCommentFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();
		return s;
	}

	protected void fillBlogCommentFromResultSet(BlogComment s, ResultSet rs) throws Exception{
		s.setAGENT(rs.getString("AGENT"));
		s.setAPPROVED(rs.getString("APPROVED"));
		s.setAUTHOR(rs.getString("AUTHOR"));
		s.setAUTHOR_EMAIL(rs.getString("AUTHOR_EMAIL"));
		s.setAUTHOR_IP(rs.getString("AUTHOR_IP"));
		s.setAUTHOR_URL(rs.getString("AUTHOR_URL"));
		s.setCONTENT(rs.getString("CONTENT"));
		s.setDATE(rs.getTimestamp("DATE"));
		s.setID(rs.getInt("ID"));
		s.setPARENT(rs.getInt("PARENT"));
		s.setPOST_ID(rs.getInt("POST_ID"));
		s.setTYPE(rs.getString("TYPE"));
		s.setUSER_ID(rs.getInt("USER_ID"));
		
	}
	public void update(BlogComment bc) throws Exception{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogCommentModel.update"));
		p.setString(1,bc.getAGENT());
		p.setString(2,bc.getAPPROVED());
		p.setString(3,bc.getAUTHOR());
		p.setString(4,bc.getAUTHOR_EMAIL());
		p.setString(5,bc.getAUTHOR_IP());
		p.setString(6,bc.getAUTHOR_URL());
		p.setString(7,bc.getCONTENT());
		p.setTimestamp(8,new Timestamp(bc.getDATE().getTime()));
		p.setInt(9,bc.getPARENT());
		p.setInt(10,bc.getPOST_ID());
		p.setString(11,bc.getTYPE());
		p.setInt(12,bc.getUSER_ID());
		p.setInt(13,bc.getID());
		p.executeUpdate();
		p.close();
	}
	public int addNew(BlogComment bp) throws Exception{
		PreparedStatement p = this.getStatementForAutoKeys("BlogCommentModel.addNew");
		setAutoGeneratedKeysQuery(SystemGlobals.getSql("BlogCommentModel.lastGeneratedBlogCommentID"));
		initBlogComment(bp, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	protected void initBlogComment(BlogComment bc, PreparedStatement p) throws Exception{
		p.setString(1,bc.getAGENT());
		p.setString(2,bc.getAPPROVED());
		p.setString(3,bc.getAUTHOR());
		p.setString(4,bc.getAUTHOR_EMAIL());
		p.setString(5,bc.getAUTHOR_IP());
		p.setString(6,bc.getAUTHOR_URL());
		p.setString(7,bc.getCONTENT());
		p.setTimestamp(8,new Timestamp(bc.getDATE().getTime()));
		p.setInt(9,bc.getPARENT());
		p.setInt(10,bc.getPOST_ID());
		p.setString(11,bc.getTYPE());
		p.setInt(12,bc.getUSER_ID());
		
	}

	public List selectByPostID(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogCommentModel.selectByPOST_ID"));
		p.setInt(1,id);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectAllByPostID(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogCommentModel.selectAllByPOST_ID"));
		p.setInt(1,id);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			BlogComment u = new BlogComment();
			fillBlogCommentFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	public void delete(BlogComment bc) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogCommentModel.delete"));
		p.setInt(1,bc.getID());
		p.executeUpdate();
		
	}
}
